namespace C1.Win.C1FlexGrid
{
    using C1.Win.C1FlexGrid.Util.Styles;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;

    public abstract class RowCol
    {
        internal RowColCollection _coll;
        internal string _comboList;
        internal int _dataIndex;
        internal IDictionary _dataMap;
        internal Type _dataType;
        internal string _editMask;
        internal RowColFlags _flags;
        internal string _format;
        internal IDictionary _imageMap;
        internal int _offset;
        internal int _size = -1;
        internal C1.Win.C1FlexGrid.Util.Styles.Style _style;
        internal C1.Win.C1FlexGrid.Util.Styles.Style _styleFixed;
        internal object _userData;
        internal const string DOUBLEQUOTE = "\"\"";
        internal const string QUOTE = "\"";

        protected internal RowCol()
        {
        }

        internal bool ChangeFlags(RowColFlags flags, bool flagsOn)
        {
            RowColFlags flags2 = this._flags;
            if (flagsOn)
            {
                this._flags = (RowColFlags) ((short) (this._flags | flags));
            }
            else
            {
                this._flags = (RowColFlags) ((short) (this._flags + ((short) ~flags)));
            }
            return (this._flags != flags2);
        }

        public virtual void Clear(ClearFlags clear)
        {
            if ((clear & ClearFlags.UserData) != 0)
            {
                this._userData = null;
            }
            if ((clear & ClearFlags.Content) != 0)
            {
                this._dataType = null;
                this._editMask = (string) (this._comboList = null);
                this._imageMap = (IDictionary) (this._dataMap = null);
            }
            if ((clear & ClearFlags.Style) != 0)
            {
                this._format = null;
                this._style = (C1.Win.C1FlexGrid.Util.Styles.Style) (this._styleFixed = null);
            }
            this._flags = 0;
            this._size = -1;
            this.SetDirty();
        }

        internal RowCol Detach()
        {
            this._coll = null;
            return this;
        }

        internal void SetDirty()
        {
            if (this._coll != null)
            {
                this._coll.SetDirty();
            }
        }

        internal void SetFlags(RowColFlags flags, bool flagsOn)
        {
            if (flagsOn)
            {
                this._flags = (RowColFlags) ((short) (this._flags | flags));
            }
            else
            {
                this._flags = (RowColFlags) ((short) (this._flags + ((short) ~flags)));
            }
        }

        [DefaultValue(true)]
        public virtual bool AllowDragging
        {
            get
            {
                return (((short) (this._flags & RowColFlags.NoDrag)) == 0);
            }
            set
            {
                if (this.AllowDragging != value)
                {
                    this.SetFlags(RowColFlags.NoDrag, !value);
                }
            }
        }

        [DefaultValue(true)]
        public virtual bool AllowEditing
        {
            get
            {
                return (((short) (this._flags & RowColFlags.NoEdit)) == 0);
            }
            set
            {
                if (this.AllowEditing != value)
                {
                    this.SetFlags(RowColFlags.NoEdit, !value);
                }
            }
        }

        [DefaultValue(false)]
        public virtual bool AllowMerging
        {
            get
            {
                return (((short) (this._flags & RowColFlags.Merge)) != 0);
            }
            set
            {
                if (this.AllowMerging != value)
                {
                    this.SetFlags(RowColFlags.Merge, value);
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        [DefaultValue(true)]
        public virtual bool AllowResizing
        {
            get
            {
                return (((short) (this._flags & RowColFlags.NoResize)) == 0);
            }
            set
            {
                if (this.AllowResizing != value)
                {
                    this.SetFlags(RowColFlags.NoResize, !value);
                }
            }
        }

        internal int Beg
        {
            get
            {
                if (this._coll == null)
                {
                    return 0;
                }
                if (this._coll._dirty)
                {
                    this._coll.Update();
                }
                return this._offset;
            }
        }

        [DefaultValue((string) null)]
        public string ComboList
        {
            get
            {
                return this._comboList;
            }
            set
            {
                if (this._comboList != value)
                {
                    this._comboList = value;
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        public IDictionary DataMap
        {
            get
            {
                return this._dataMap;
            }
            set
            {
                if (this._dataMap != value)
                {
                    this._dataMap = value;
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        public virtual Type DataType
        {
            get
            {
                return this._dataType;
            }
            set
            {
                if (value == typeof(object))
                {
                    value = null;
                }
                if (this._dataType != value)
                {
                    this._dataType = value;
                    if ((value == typeof(bool)) || (value == typeof(Image)))
                    {
                        this.ImageAlign = ImageAlignEnum.CenterCenter;
                    }
                    else if (value == typeof(string))
                    {
                        this.TextAlign = TextAlignEnum.LeftCenter;
                    }
                    else if (value == typeof(DateTime))
                    {
                        this.TextAlign = TextAlignEnum.LeftCenter;
                        this.Format = "d";
                    }
                    else if ((value != null) && value.IsAssignableFrom(typeof(int)))
                    {
                        this.TextAlign = TextAlignEnum.RightCenter;
                    }
                    if ((this.Format == "d") && (value != typeof(DateTime)))
                    {
                        this.Format = null;
                    }
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        [DefaultValue((string) null)]
        public string EditMask
        {
            get
            {
                return this._editMask;
            }
            set
            {
                if (this._editMask != value)
                {
                    this._editMask = value;
                }
            }
        }

        internal int End
        {
            get
            {
                return (this.Beg + this.SizeDisplay);
            }
        }

        [DefaultValue((string) null)]
        public string Format
        {
            get
            {
                return this._format;
            }
            set
            {
                if (this._format != value)
                {
                    this._format = value;
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        [DefaultValue(1)]
        public ImageAlignEnum ImageAlign
        {
            get
            {
                return this.StyleDisplay.ImageAlign;
            }
            set
            {
                this.StyleNew.ImageAlign = value;
            }
        }

        public bool ImageAndText
        {
            get
            {
                return (((short) (this._flags & RowColFlags.ImageAndText)) != 0);
            }
            set
            {
                if (this.ImageAndText != value)
                {
                    this.SetFlags(RowColFlags.ImageAndText, value);
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        public IDictionary ImageMap
        {
            get
            {
                return this._imageMap;
            }
            set
            {
                if (this._imageMap != value)
                {
                    this._imageMap = value;
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        public virtual int Index
        {
            get
            {
                if (this._coll != null)
                {
                    return this._coll._items.IndexOf(this);
                }
                return -1;
            }
        }

        public virtual bool Selected
        {
            get
            {
                return (((short) (this._flags & RowColFlags.Selected)) != 0);
            }
            set
            {
                if (this.Selected != value)
                {
                    this.SetFlags(RowColFlags.Selected, value);
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        internal int Size
        {
            get
            {
                return this._size;
            }
            set
            {
                if (this._size != value)
                {
                    this._size = value;
                    this.SetDirty();
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.GridChanged);
                    }
                }
            }
        }

        internal int SizeDisplay
        {
            get
            {
                if (!this.Visible)
                {
                    return 0;
                }
                if (this._coll == null)
                {
                    return Math.Max(0, this._size);
                }
                int num = (this._size < 0) ? this._coll._defaultSize : this._size;
                if (num < this._coll._minSize)
                {
                    num = this._coll._minSize;
                }
                if ((this._coll._maxSize > 0) && (num > this._coll._maxSize))
                {
                    num = this._coll._maxSize;
                }
                return num;
            }
            set
            {
                this.Visible = true;
                this.Size = value;
            }
        }

        public virtual CellStyle Style
        {
            get
            {
                if (this._style == null)
                {
                    return null;
                }
                return new CellStyle(this._style);
            }
            set
            {
                C1.Win.C1FlexGrid.Util.Styles.Style style = (value == null) ? null : value._style;
                if (style != this._style)
                {
                    this._style = style;
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        public virtual CellStyle StyleDisplay
        {
            get
            {
                C1.Win.C1FlexGrid.Util.Styles.Style style = this._style;
                if ((style == null) && (this._coll != null))
                {
                    style = this._coll.GetStyle(CellStyleEnum.Normal);
                }
                if (style != null)
                {
                    return new CellStyle(style);
                }
                return null;
            }
        }

        public virtual CellStyle StyleFixed
        {
            get
            {
                if (this._styleFixed == null)
                {
                    return null;
                }
                return new CellStyle(this._styleFixed);
            }
            set
            {
                if (((this._styleFixed != null) || (value != null)) && (this._styleFixed != value._style))
                {
                    this._styleFixed = (value == null) ? null : value._style;
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.RepaintGrid);
                    }
                }
            }
        }

        public virtual CellStyle StyleFixedDisplay
        {
            get
            {
                C1.Win.C1FlexGrid.Util.Styles.Style style = this._styleFixed;
                if ((style == null) && (this._coll != null))
                {
                    style = this._coll.GetStyle(CellStyleEnum.Fixed);
                }
                if (style != null)
                {
                    return new CellStyle(style);
                }
                return null;
            }
        }

        public virtual CellStyle StyleFixedNew
        {
            get
            {
                if ((this._styleFixed == null) && (this._coll != null))
                {
                    this._styleFixed = this._coll.AddStyle();
                }
                if (this._styleFixed != null)
                {
                    return new CellStyle(this._styleFixed);
                }
                return null;
            }
        }

        public virtual CellStyle StyleNew
        {
            get
            {
                if ((this._style == null) && (this._coll != null))
                {
                    this._style = this._coll.AddStyle();
                }
                if (this._style != null)
                {
                    return new CellStyle(this._style);
                }
                return null;
            }
        }

        [DefaultValue(10)]
        public TextAlignEnum TextAlign
        {
            get
            {
                return this.StyleDisplay.TextAlign;
            }
            set
            {
                this.StyleNew.TextAlign = value;
            }
        }

        [DefaultValue(10)]
        public TextAlignEnum TextAlignFixed
        {
            get
            {
                return this.StyleFixedDisplay.TextAlign;
            }
            set
            {
                this.StyleFixedNew.TextAlign = value;
            }
        }

        public virtual object UserData
        {
            get
            {
                return this._userData;
            }
            set
            {
                this._userData = value;
            }
        }

        [DefaultValue(true)]
        public virtual bool Visible
        {
            get
            {
                return (((short) (this._flags & RowColFlags.Hidden)) == 0);
            }
            set
            {
                if (this.Visible != value)
                {
                    this.SetFlags(RowColFlags.Hidden, !value);
                    this.SetDirty();
                    if (this._coll != null)
                    {
                        this._coll.NotifyViews(GridChangedTypeEnum.GridChanged);
                    }
                }
            }
        }
    }
}

